/**
 * perm 操作权限处理
 * 指令用法：
 *  <el-button v-perms="['auth/edit']">编辑</el-button>
 */

import {getStore} from "@/api/utils";
import {Message} from "view-ui-plus";

const permission = (el, bindlings) => {
    // const {proxy} = getCurrentInstance();
    const permissions = getStore('permissions') ?? [];
    const {value} = bindlings;
    const all_permission = '*';
    if (Array.isArray(value)) {
        if (value.length > 0) {
            const hasPermission = permissions.some((key) => {
                return all_permission == key || value.includes(key);
            });
            if (!hasPermission) {
                console.log(el)
                // 禁用按钮
                // el.style.cursor = 'not-allowed';
                // el.pointerEvents = 'none';
                // el.disabled = true;
                el.addEventListener('click', function (e) {
                    Message.warning('权限不足!');
                    e.stopPropagation();
                }, true);
            }
        }
    } else {
        throw new Error('like v-perms="[\'config/edit\']"');
    }
}

export default permission;
